<?php
/**
 * 成员管理  控制器
 */

namespace App\Http\Controllers\UserCenter;


use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getUserInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required_without:uuid|int',
            'uuid' => 'required_without:id|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getUserInfo($validator->getData());
    }

    /**
     * 用户登录
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function loginMember(Request $request)
    {
        //验证规则
        $rules = [
            'member_name' => 'required_without_all:member_phone,member_email|string',
            'member_phone' => 'required_without_all:member_name,member_email|string',
            'member_email' => 'required_without_all:member_phone,member_name|string',
            'member_password' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->loginMember($validator->getData());
    }

    /**
     * 添加
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addUser(Request $request)
    {
        //验证规则
        $rules = [
            'role_id' => 'required|int',
            'member_name' => 'required|string',
            'member_password' => 'required|string',
            'member_email' => 'nullable|string|email',
            'member_phone' => 'nullable|string',
        ];
        $messages = [
//            'member_name.unique:userCenter.account' => '用户名已存在',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addUser($validator->getData());
    }

    public function loginOut(Request $request)
    {
        //验证规则
        $rules = [
//            'token' => 'required|string',
        ];
        $messages = [
//            'member_name.unique:userCenter.account' => '用户名已存在',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->loginOut($validator->getData());
    }

    /**
     * 获取用户列表
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getUserList(Request $request)
    {
        //验证规则
        $rules = [
            'type' => 'nullable|int|in:1,2',
            'member_name' => 'nullable|string',
            'member_email' => 'nullable|string',
            'member_phone' => 'nullable|string',
            'status' => 'nullable|int',
        ];
        $messages = [

        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getUserList($validator->getData());
    }

    public function editUser(Request $request)
    {
        $rules = [
            'id' => 'required|int',
            'member_name' => 'nullable|string',
            'member_password' => 'nullable|string',
            'member_email' => 'nullable|email',
            'member_phone' => 'nullable',
            'status' => 'nullable|int|in:1,2',

            'userInfo' => 'nullable|array',
            'user_info.account_id' => 'nullable|int',
            'user_info.img' => 'nullable|string',
            'user_info.thumbnail' => 'nullable|string',
            'user_info.address' => 'nullable|string',
            'user_info.department_id' => 'nullable|int',
            'user_info.role_id' => 'nullable|string',
        ];
        $messages = [];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editUser($validator->getData());
    }

    public function delUser(Request $request)
    {
        $rules = [
            'ids' => 'required|string'
        ];
        $messages = [];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delUser($validator->getData());
    }
}
